/// Creator:      gaoruishan
/// CreatDate:    2025-05-23
/// Description:  病人信息相关
Class Nur.MNISV3.Service.Patient Extends %RegisteredObject [ ClassType = "", Not ProcedureBlock ]
{

/// 床位图列表
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Patient).GetBedMapPatientsV4("3","20665","","8","")) 
ClassMethod GetBedMapPatientsV4(wardId As %String, userId As %String, module As %String = "", locId As %String = "", partial = "")
{
    n (wardId,userId,module,locId,partial,%session)
    s $zt="errGetBedMapPatientsV4"
    s ^GRS("GetBedMapPatientsV4")=$lb(wardId,userId,module,locId,partial)
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    //调用纯净版患者列表，
    s patListObj=##class(%ResultSet).%New("Nur.MNISV3.Service.Patient:GetPatList")
    d patListObj.Execute(wardId,userId,module,locId)
    s patInfoList=##class(%ListOfDataTypes).%New()
    s screeningItems=##class(%ListOfDataTypes).%New()
    while(patListObj.Next()){
       s patinfo=##class(%ArrayOfDataTypes).%New()
       f len=1:1:patListObj.GetColumnCount()
       {
             s ColumnName=patListObj.GetColumnName(len) 
             s Value=patListObj.GetDataByName(ColumnName)
             d patinfo.SetAt(Value,ColumnName)
       }
       s episodeId=patListObj.GetDataByName("episodeId")
       //往arr里添加需要的患者信息
       s regNo=##class(Nur.NIS.Service.Base.Patient).GetRegNo(episodeId)
       d patinfo.SetAt(regNo,"regNo")
       s diagnosis=##class(Nur.NIS.Service.Base.Patient).GetDiagnosis(episodeId)
       d patinfo.SetAt(diagnosis,"diagnosis")
       s inHospDateTime=##class(Nur.NIS.Service.Base.Patient).GetInHospDateTime(episodeId)
       d patinfo.SetAt(inHospDateTime,"inHospDateTime")
       s inDays=##class(Nur.NIS.Service.Base.Patient).PatInDays(episodeId)
       d patinfo.SetAt(inDays,"inDays")
       s admReason=##class(Nur.NIS.Service.Base.Patient).GetAdmReason(episodeId)
       d patinfo.SetAt(admReason,"admReason")
       s mainDoctor=##class(Nur.NIS.Service.Base.Patient).GetMainDoctor(episodeId)
       d patinfo.SetAt(mainDoctor,"mainDoctor")
       s careLevelColor=##class(Nur.NIS.Service.Base.Patient).GetCareLevelColor(episodeId)
       d patinfo.SetAt(careLevelColor,"careLevelColor")
       s medicareNo=##class(Nur.NIS.Service.Base.Patient).GetMedicareNo(episodeId)
       d patinfo.SetAt(medicareNo,"medicareNo")
       s patName=##class(Nur.NIS.Service.Base.Patient).GetName(episodeId)
       d patinfo.SetAt(patName,"name")
       s sex=##class(Nur.NIS.Service.Base.Patient).GetSex(episodeId)
       d patinfo.SetAt(sex,"sex")
       s bedCode=##class(Nur.NIS.Service.Base.Patient).GetBedCode(episodeId)
       d patinfo.SetAt(bedCode,"bedCode")
       
       s patSkinOrd=..GetSkinOrd(episodeId)
       d patinfo.SetAt(patSkinOrd,"skintests")
       //s transRecList=##class(Nur.MNIS.Service.Patient).GetTransRecord(episodeId)
       //d patinfo.SetAt(transRecList,"transRecList")
       d patInfoList.Insert(patinfo)
    }
    //状态
    i module'="" s topFilterCondition="inBedAll$全区^manageInBed$管辖^wait$等候"
    e  s topFilterCondition="inBedAll$全区^manageInBed$管辖^todayOut$今日出院^allOut$已出院^wait$等候"
    s topFilterList=##class(%ListOfDataTypes).%New()
    f len=1:1:$L(topFilterCondition,"^") d
    .s topFilterCode=$P($P(topFilterCondition,"^",len),"$",1)
    .s topFilterDesc=$P($P(topFilterCondition,"^",len),"$",2)
    .s topFilterobj=##class(%ArrayOfDataTypes).%New()
    .d topFilterobj.SetAt(topFilterCode,"code")
    .d topFilterobj.SetAt(topFilterDesc,"name")
    .d topFilterList.Insert(topFilterobj)
    s status=##class(%ArrayOfDataTypes).%New()
    d status.SetAt("状态","title")
    d status.SetAt("status","type")
    d status.SetAt(topFilterList,"items")
    d screeningItems.Insert(status)
    
    //筛选
    s bedListScreenStr=##class(Nur.MNISV3.Service.Config).GetBedListLeftScreenStr("",locId,"",module)
    s bedListScreenLen=$l(bedListScreenStr,"^")
    s leftFilterList=##class(%ListOfDataTypes).%New()
    f bedListScreenInd=1:1:bedListScreenLen d
    .s bedListScreenNode=$p(bedListScreenStr,"^",bedListScreenInd)
    .s bedListScreenNodeCode=$p(bedListScreenNode,"|",1)
    .s bedListScreenNodeName=$p(bedListScreenNode,"|",2)
    .s leftFilter=##class(%ArrayOfDataTypes).%New()
    .d leftFilter.SetAt(bedListScreenNodeCode,"code")
    .d leftFilter.SetAt(bedListScreenNodeName,"name")
    .d leftFilterList.Insert(leftFilter)
    s quickScreening=##class(%ArrayOfDataTypes).%New()
    d quickScreening.SetAt("筛选","title")
    d quickScreening.SetAt("quickScreening","type")
    d quickScreening.SetAt(leftFilterList,"items")
    d screeningItems.Insert(quickScreening)
    
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(patInfoList),"patients")
    d json.SetAt($g(screeningItems),"screeningItems")
    q json        
errGetBedMapPatientsV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100021","-1")
}

/// 基本患者信息--支持patinfo空 则创建并返回
ClassMethod GetBasicInfo(patinfo As %ArrayOfDataTypes, episodeId = "")
{
    n (patinfo,episodeId)
    //如果patinfo空，则创建
    i '$isobject(patinfo) s patinfo=##class(%ArrayOfDataTypes).%New()
    i episodeId="" q patinfo
     //基本患者信息
    s regNo=##class(Nur.NIS.Service.Base.Patient).GetRegNo(episodeId)
    d patinfo.SetAt(regNo,"regNo")
    s medicareNo=##class(Nur.NIS.Service.Base.Patient).GetMedicareNo(episodeId)
    d patinfo.SetAt(medicareNo,"medicareNo")
    s patName=##class(Nur.NIS.Service.Base.Patient).GetName(episodeId)
    d patinfo.SetAt(patName,"name")
    s sex=##class(Nur.NIS.Service.Base.Patient).GetSex(episodeId)
    d patinfo.SetAt(sex,"sex")
    s bedCode=##class(Nur.NIS.Service.Base.Patient).GetBedCode(episodeId)
    s inHospDateTime=##class(Nur.NIS.Service.Base.Patient).GetInHospDateTime(episodeId)
    d patinfo.SetAt(inHospDateTime,"inHospDateTime")
    d patinfo.SetAt(bedCode,"bedCode")
    d patinfo.SetAt(episodeId,"episodeID")
    q patinfo
}

/// 患者图标 及其他
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Patient).GetBedMapAdditionsV4("2^46")) 
ClassMethod GetBedMapAdditionsV4(episodeIdStr = "")
{
    n (episodeIdStr)
    s $zt="errGetBedMapAdditionsV4"
    s ^GRS("GetBedMapAdditionsV4")=episodeIdStr
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    s patInfoList=##class(%ListOfDataTypes).%New()
    s codeStr="^gotAllergy^NewPatient^FeverInThreeDay^Dangerous^SERIOUSLY^TodayOperation^DHCAbnormalReport^DocDisch^arreag^EpdNotReport^EpdReport^NewTransInPat^"
    s len=$s(episodeIdStr'="":$l(episodeIdStr,"^"),1:0)
    f ind=1:1:len d
    .s episodeId=$p(episodeIdStr,"^",ind)
    .q:(episodeId="")
    .s patinfo=##class(%ArrayOfDataTypes).%New()
    .s icons=..GetIcons(episodeId,codeStr)
    .d patinfo.SetAt(icons,"icons")
    .d patinfo.SetAt(episodeId,"episodeId")
    .d patInfoList.Insert(patinfo)
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(patInfoList),"patients")
    q json
errGetBedMapAdditionsV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100021","-1")
}

/// 图标
ClassMethod GetIcons(episodeId, ctiaCodeStr = "") As %String
{
    n (episodeId,ctiaCodeStr,%session)
    s Icons=##class(%ListOfDataTypes).%New()
    s ctipId=$o(^epr.CTIconProfileI("UCODE","MOBILENURSE",""))
    i (ctipId="") s ctipId=$o(^epr.CTIconProfileD(""))
    s ctipObj=##Class(epr.CTIconProfile).%OpenId(ctipId)
    i ($isobject(ctipObj)) {
        s ctipLen=ctipObj.Items.Count()
        f ctipInd=1:1:ctipLen {
            s ctipiObj=ctipObj.Items.GetAt(ctipInd)
            s ctipiActive=ctipiObj.Active
            s ctiaObj=ctipiObj.IconDR
            continue:(ctipiActive=0)||('$isobject(ctiaObj))
            s ctiaCode=ctiaObj.Code
            s ctiaDesc=ctiaObj.Description
            s ctiaExpr=ctiaObj.CondExpr
            s ctiaIcon=ctiaObj.Icon
            continue:(ctiaCode="")||(ctiaExpr="")
            continue:(ctiaCodeStr'="")&&(("^"_ctiaCodeStr_"^")'[("^"_ctiaCode_"^"))
            s EpisodeID=episodeId,img=0,title=""
            try {
                x ctiaExpr
            } catch {
                s img=0
            }
            continue:('img)
            s icon=##class(%ArrayOfDataTypes).%New()
            d icon.SetAt(ctiaIcon,"icon")
            d icon.SetAt(ctiaCode,"code")
            d icon.SetAt(ctiaDesc,"desc")
            d Icons.Insert(icon)
        }
    }
    q Icons
}

/// 患者详情
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Patient).GetBedMapPatientV4("2")) 
ClassMethod GetBedMapPatientV4(episodeId = "")
{
    n (episodeId)
    s $zt="errGetBedMapPatientV4"
    s ^GRS("GetBedMapPatientV4")=episodeId
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    s patInfoObj=##class(Nur.MNISV3.Service.PatientInfo).GetPatient(episodeId)
    s transRecList=..GetTransRecord(episodeId)
    //^CT.NUR.NIS.NurseBasicDataD
    s codeStr = "episodeID^age^bedCode^inDays^sex^name^patCredNoImg^wardDesc^inHospDateTime^balance^ifCanTransBabyBed^latestTransLocDateTime^weekDays^illState"
    s columns=##class(%ListOfDataTypes).%New()
    s id="" f  s id=$o(^CT.NUR.NIS.NurseBasicDataD(id)) q:(id="")  d
    .s data=$g(^CT.NUR.NIS.NurseBasicDataD(id))
    .q:($lg(data,7)=1) //过滤测试的
    .s code=$lg(data,2)
    .s name=$lg(data,4)
    .s type=$lg(data,3)
    .q:(type'=0)||(codeStr'[code) //包含的
    .s value=patInfoObj.GetAt(code)
    .s column=##class(%ArrayOfDataTypes).%New()
    .d column.SetAt($g(code),"key")
    .d column.SetAt($g(name),"title")
    .d column.SetAt($g(value),"value")
    .d columns.Insert(column)
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt(columns,"columns")
    d json.SetAt(transRecList,"transInfo")
    d json.SetAt($g(patInfoObj),"patInfo")
    q json
errGetBedMapPatientV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100021","-1")
}

/// 患者列表(纯净,不带患者信息)   如果module="" 查询出院患者
/// d ##class(%ResultSet).RunQuery("Nur.MNISV3.Service.Patient","GetPatList","21","10211","","")
Query GetPatList(wardId As %String, userId As %String, module As %String = "", locId As %String = "") As %Query(ROWSPEC = "ind,episodeId,inBedAll,todayOut,allOut,wait")
{
}

ClassMethod GetPatListExecute(ByRef qHandle As %Binary, wardId As %String, userId As %String, module As %String = "", locId As %String = "") As %Status
{
    Set repid=$I(^CacheTemp)
    s ind=1
    k tmpSort
    ///在床患者
    s curRoomId="" f  s curRoomId=$o(^PAADMi("CurrWard",wardId,curRoomId)) q:curRoomId=""  d
    .s EpisodeID=0 f  s EpisodeID=$o(^PAADMi("CurrWard",wardId,curRoomId,EpisodeID)) q:EpisodeID=""  d
    ..s pavisit=$p($g(^PAADM(EpisodeID)),"^",20)
    ..i pavisit'="A" q
    ..s bedSub=$p($g(^PAADM(EpisodeID)),"^",73)
    ..q:bedSub=""
    ..s curWardId=$P(bedSub,"||",1)
    ..s curBedSub=$P(bedSub,"||",2)
    ..q:(curWardId="")||(curBedSub="")
    ..s bedX=$p($g(^PAWARD(curWardId,"BED",curBedSub)),"^",15)
    ..s bedY=$p($g(^PAWARD(curWardId,"BED",curBedSub)),"^",16)
    ..s bedCode=$p($g(^PAWARD(curWardId,"BED",curBedSub)),"^",1)
    ..s motherbedX="",motherbedY=""
    ..i (bedY="")||(bedX="") 
    ..;s bedX = curWardId , bedY = +bedCode 
    ..i bedX=""||(bedY="") s bedX=curWardId,bedY=curBedSub
    ..q:(bedY="")||(bedX="")
    ..s PatientID=+^PAADM(+EpisodeID)
    ..s seqNo=$p($g(^PAWARD(curWardId,"BED",curBedSub)),"^",24)  //获取顺序号
    ..s motherEpisodeID=$p($g(^PAADM(EpisodeID)),"^",75) 
    ..i motherEpisodeID'="" d
    ...s motherBedSub=$p($g(^PAADM(motherEpisodeID)),"^",73)
    ...q:motherBedSub'["||"
    ...s pavisit=$p($g(^PAADM(motherEpisodeID)),"^",20)
    ...i pavisit'="A" q
    ...s motherbedX=$p($g(^PAWARD($P(motherBedSub,"||",1),"BED",$P(motherBedSub,"||",2))),"^",15)
    ...s motherbedY=$p($g(^PAWARD($P(motherBedSub,"||",1),"BED",$P(motherBedSub,"||",2))),"^",16)
    ...s seqNo=$p($g(^PAWARD($P(motherBedSub,"||",1),"BED",$P(motherBedSub,"||",2))),"^",24)  //获取母亲顺序号
    ...i motherbedX=""||(motherbedY="") s motherbedX=curWardId,motherbedY=$P(motherBedSub,"||",2) // EH
    ..;s seqNo=+seqNo
    ..i seqNo="" s seqNo="最后"
    ..d saveInfo
    ;b
    s seqNo=""
    f  s seqNo=$o(tmpSort(seqNo)) q:seqNo=""  d
    .s cbedY=""
    .f  s cbedY=$O(tmpSort(seqNo,cbedY)) q:cbedY=""  d
    ..s cbedX=""
    ..f  s cbedX=$O(tmpSort(seqNo,cbedY,cbedX)) q:cbedX=""  d
    ...s episodeId=$G(tmpSort(seqNo,cbedY,cbedX))
    ...i episodeId'="" d
    ....d initValue
    ....s inBedAll=1
    ....d OutRowPatList
    ...s cmotherbedY=""
    ...f  s cmotherbedY=$O(tmpSort(seqNo,cbedY,cbedX,cmotherbedY)) q:cmotherbedY=""  d
    ....s cmotherbedX=""
    ....f  s cmotherbedX=$O(tmpSort(seqNo,cbedY,cbedX,cmotherbedY,cmotherbedX)) q:cmotherbedX=""  d
    .....s episodeId=$G(tmpSort(seqNo,cbedY,cbedX,cmotherbedY,cmotherbedX))
    .....i episodeId'="" d
    ......d initValue
    ......s inBedAll=1
    ......d OutRowPatList
     
    ///等候区患者
    s wardAdmid=0 f  s wardAdmid=$o(^PAWARDA(wardId,"WADM",wardAdmid)) q:wardAdmid=""  d
    .s episodeId=$p(^PAWARDA(wardId,"WADM",wardAdmid),"^",1)
    .s pavisit=$p($g(^PAADM(episodeId)),"^",20)
    .s chargeState=##class(web.DHCDischargeHistory).GetCurrentDischargeStatus(episodeId)
    .q:(pavisit="D")&(chargeState'="B")
    .q:(pavisit'="P")&(pavisit'="A")&(chargeState'="B")   
    .s admType=$p(^PAADM(episodeId),"^",2)
    .q:admType'="I"
    .d initValue
    .s wait=1
    .d OutRowPatList
    
    s outDayNum=##class(Nur.MNISV3.Service.Config).GetGeneralSettingValue("","",wardId,"","","AllOutNum")
    i outDayNum="" s outDayNum=7
    i module=""
    {
        ///出院患者 前期后台写死全出查询7天的患者
        s curdate=+$H
        s episodeId="" f  s episodeId=$o(^PAADMi("EstDisch",curdate,episodeId)) q:(episodeId="")  d // EH 医疗结算
        .s pavisit=$p($g(^PAADM(episodeId)),"^",20)
        .q:pavisit="D" 
        .s curWardId=$p($g(^PAADM(episodeId)),"^",70)
        .q:wardId'=curWardId
        .d initValue
        .s todayOut=1
        .d OutRowPatList
        f cdate=(curdate-outDayNum):1:curdate  d
        .s episodeId=""
        .f  s episodeId=$o(^PAADMi("DisDateT","I",cdate,episodeId)) q:episodeId=""  d
        ..s pavisit=$p($g(^PAADM(episodeId)),"^",20)
        ..q:pavisit'="D" 
        ..s curWardId=$p($g(^PAADM(episodeId)),"^",70)
        ..q:wardId'=curWardId
        ..s bedSub=$p($g(^PAADM(episodeId)),"^",73)
        ..d initValue
        ..;i cdate=curdate s todayOut=1
        ..s allOut=1
        ..d OutRowPatList
    }
    Set qHandle=$lb(0,repid,0)
    Quit $$$OK
saveInfo
    i ($G(motherbedX)'="")&($G(motherbedY)'="")  s tmpSort(seqNo,motherbedY,motherbedX,bedY,bedX)=EpisodeID
    e  s tmpSort(seqNo,bedY,bedX)=EpisodeID
    quit
initValue
  s (inBedAll,todayOut,allOut,wait)=0
  quit
OutRowPatList
    set Data=$lb(ind,episodeId,inBedAll,todayOut,allOut,wait)
    Set ^CacheTemp(repid,ind)=Data
    Set ind=ind+1
    quit
}

ClassMethod GetPatListFetch(ByRef qHandle As %Binary, ByRef Row As %List, ByRef AtEnd As %Integer = 0) As %Status [ PlaceAfter = GetPatListExecute ]
{
    Set AtEnd=$LIST(qHandle,1)
    Set repid=$LIST(qHandle,2)
    Set ind=$LIST(qHandle,3)
    Set ind=$o(^CacheTemp(repid,ind))
    If ind="" {             // if there are no more rows, finish fetching
        Set AtEnd=1
        Set Row=""
    }
    Else      {         
        Set Row=^CacheTemp(repid,ind)
    }
    s qHandle=$lb(AtEnd,repid,ind)
    Quit $$$OK
}

ClassMethod GetPatListClose(ByRef qHandle As %Binary) As %Status [ PlaceAfter = GetPatListExecute ]
{
    Set repid=$LIST(qHandle,2)
    Kill ^CacheTemp(repid)
    Quit $$$OK
}

/// 获取病人列表
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Patient).GetWardPatList("3","20665","","8","")) 
ClassMethod GetWardPatList(wardId As %String, userId As %String, module As %String = "", locId As %String = "")
{
    n (wardId,userId,module,locId)
    s $zt="errGetWardPatList"
    s ^GRS("GetWardPatList")=$lb(wardId,userId,module,locId)
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    //调用纯净版患者列表，
    s patListObj=##class(%ResultSet).%New("Nur.MNISV3.Service.Patient:GetPatList")
    d patListObj.Execute(wardId,userId,module,locId)
    s patInfoList=##class(%ListOfDataTypes).%New()
    s screeningItems=##class(%ListOfDataTypes).%New()
    while(patListObj.Next()){
       s patinfo=##class(%ArrayOfDataTypes).%New()
       f len=1:1:patListObj.GetColumnCount()
       {
             s ColumnName=patListObj.GetColumnName(len) 
             s Value=patListObj.GetDataByName(ColumnName)
             d patinfo.SetAt(Value,ColumnName)
       }
       s episodeId=patListObj.GetDataByName("episodeId")
       //往arr里添加需要的患者信息
       s regNo=##class(Nur.NIS.Service.Base.Patient).GetRegNo(episodeId)
       d patinfo.SetAt(regNo,"regNo")
       s diagnosis=##class(Nur.NIS.Service.Base.Patient).GetDiagnosis(episodeId)
       d patinfo.SetAt(diagnosis,"diagnosis")
       s inHospDateTime=##class(Nur.NIS.Service.Base.Patient).GetInHospDateTime(episodeId)
       d patinfo.SetAt(inHospDateTime,"inHospDateTime")
       s inDays=##class(Nur.NIS.Service.Base.Patient).PatInDays(episodeId)
       d patinfo.SetAt(inDays,"inDays")
       s admReason=##class(Nur.NIS.Service.Base.Patient).GetAdmReason(episodeId)
       d patinfo.SetAt(admReason,"admReason")
       s mainDoctor=##class(Nur.NIS.Service.Base.Patient).GetMainDoctor(episodeId)
       d patinfo.SetAt(mainDoctor,"mainDoctor")
       s careLevelColor=##class(Nur.NIS.Service.Base.Patient).GetCareLevelColor(episodeId)
       d patinfo.SetAt(careLevelColor,"careLevelColor")
       s medicareNo=##class(Nur.NIS.Service.Base.Patient).GetMedicareNo(episodeId)
       d patinfo.SetAt(medicareNo,"medicareNo")
       s patName=##class(Nur.NIS.Service.Base.Patient).GetName(episodeId)
       d patinfo.SetAt(patName,"name")
       s sex=##class(Nur.NIS.Service.Base.Patient).GetSex(episodeId)
       d patinfo.SetAt(sex,"sex")
       s bedCode=##class(Nur.NIS.Service.Base.Patient).GetBedCode(episodeId)
       d patinfo.SetAt(bedCode,"bedCode")
       
       s patSkinOrd=..GetSkinOrd(episodeId)
       d patinfo.SetAt(patSkinOrd,"skinOrd")
       d patInfoList.Insert(patinfo)
    }
    i module'="" s topFilterCondition="inBedAll$全区^manageInBed$管辖^wait$等候"
    e  s topFilterCondition="inBedAll$全区^manageInBed$管辖^todayOut$今日出院^allOut$已出院^wait$等候"
    s topFilterList=##class(%ListOfDataTypes).%New()
    f len=1:1:$L(topFilterCondition,"^") d
    .s topFilterCode=$P($P(topFilterCondition,"^",len),"$",1)
    .s topFilterDesc=$P($P(topFilterCondition,"^",len),"$",2)
    .s topFilterobj=##class(%ArrayOfDataTypes).%New()
    .d topFilterobj.SetAt(topFilterCode,"code")
    .d topFilterobj.SetAt(topFilterDesc,"desc")
    .d topFilterList.Insert(topFilterobj)
    
    s leftFilterCondition="allPat$全区^newPatient$新入^dangerous$危重^operation$手术^fever$发烧^gotAllergy$过敏"
    s leftFilterList=##class(%ListOfDataTypes).%New()
    f len=1:1:$L(leftFilterCondition,"^") d
    .s leftFilterCode=$P($P(leftFilterCondition,"^",len),"$",1)
    .s leftFilterDesc=$P($P(leftFilterCondition,"^",len),"$",2)
    .s leftFilter=##class(%ArrayOfDataTypes).%New()
    .d leftFilter.SetAt(leftFilterCode,"code")
    .d leftFilter.SetAt(leftFilterDesc,"desc")
    .d leftFilterList.Insert(leftFilter)
    
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(patInfoList),"patInfoList")
    d json.SetAt($g(topFilterList),"topFilter")
    i module="" d json.SetAt($g(leftFilterList),"leftFilter")
    q json        
errGetWardPatList
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100021","-1")
}

/// 扫描腕带获取病人信息
/// w ##class(Nur.MNISV3.Service.Patient).GetPatWristInfo("0000000689","3")
ClassMethod GetPatWristInfo(regNo As %String, wardId As %String)
{
    n (regNo,wardId)
    s $zt="errGetPatWristInfo"
    s ^GRS("GetPatWristInfo")=$lb(regNo,wardId)
    s msg="核对信息无误",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    d ##class(Nur.MNISV3.Service.Barcode).GetBarcodeFlag(regNo,"","",wardId,.regNo) // EH 条码设置
    s getPatAdmValue=..GetPatAdm(regNo)
    s episodeId=$P(getPatAdmValue,"^",1)
    s lastVisit=$P(getPatAdmValue,"^",2)
    
    i episodeId=""
    {
        s msg="未找到腕带对应的患者,请确认",msgcode="100322",Status="-1"
        d json.SetAt($g(msg),"msg")
        d json.SetAt($g(msgcode),"msgcode")
        d json.SetAt($g(Status),"status")
        q ##class(Nur.MNISV3.Common.JSON).Encode(json)
    }
    else
    {
        i ($p($g(^PAADM(episodeId)),"^",20))="D"  s msg="患者已出院！",msgcode="104999",Status="0"
    }
    
    s patWardId=$p($g(^PAADM(episodeId)),"^",70)  
    i (patWardId'=wardId)
    {
        s msg="非本病区患者",msgcode="100321",Status="-1"
    }
    else
    {
        //s patInfo=##class(Nur.MNISV3.Service.PatientInfo).GetPatient(episodeId)
        s patInfo=##class(Nur.NIS.Service.Base.Patient).GetPatient(episodeId)
        d json.SetAt(patInfo,"patInfo")
    }
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
    
errGetPatWristInfo
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100221","-1")
}

/// 根据登记号取当前在院住院就诊号
/// w ##class(Nur.MNISV3.Service.Patient).GetPatAdm("0000000129")
ClassMethod GetPatAdm(regNo)
{
    n (regNo)
    i (regNo["C") { // EH
        s admID=##class(Nur.MNISV3.Service.Barcode).GetAdmByBarcode(regNo,"")
        q admID_"^"_admID
    }
    
    s regNo=$replace(regNo,"REG","")
    s episodeId=""
    s lastVisit=""
    s lastEpisodeID=""
    s papmi=""
    f  s papmi=$o(^PAPERi("PAPMI_PatNo",regNo,papmi)) q:papmi=""  d
    .s name=$p(^PAPER(papmi,"ALL"),"^",1)
    .s Adm=""
    .f  s Adm=$o(^PAPERdr(papmi,"ADM","I",Adm)) q:Adm=""  d 
    ..s Typ=$p(^PAADM(Adm),"^",2)
    ..q:Typ'="I"    
    ..s patVisit=$p($g(^PAADM(Adm)),"^",20)
    ..s lastVisit=patVisit
    ..s lastEpisodeID=Adm
    ..q:patVisit'="A"
    ..if Adm>episodeId  s episodeId=Adm
    i episodeId=""  s episodeId=lastEpisodeID
    q episodeId_"^"_lastVisit
}

/// 获得病人的转科记录
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Patient).GetTransRecord(475))
ClassMethod GetTransRecord(episodeId)
{
    n (episodeId)
    s transRecords=##class(%ListOfDataTypes).%New() 
    s reverseTransRecords=##class(%ListOfDataTypes).%New()  
    q:episodeId="" reverseTransRecords
    q:('$d(^PAADM(episodeId,"TRANS",1))) reverseTransRecords
    s admRecord=##class(%ArrayOfDataTypes).%New()
    s admRecordTransType="入院登记" 
    s motherADM = $p(^PAADM(episodeId),"^",75)
    s ifNewBaby="N"
    i motherADM'="" s ifNewBaby="Y"
    i ifNewBaby="Y" s admRecordTransType="新生儿分娩"
    d admRecord.SetAt(admRecordTransType,"transType")
    s transGlobal=^PAADM(episodeId,"TRANS",1)
    s amdLocDR=$p(transGlobal,"^",6)  ;$p(^PAADM(episodeId),"^",4)
    d admRecord.SetAt($p($g(^SSU("SSUSR",+$p(transGlobal,"^",16))),"^",2),"transUser")  ;$p(^PAADM(episodeId),"^",43))
    d admRecord.SetAt(##class(websys.Conversions).DateLogicalToHtml($p(transGlobal,"^",1)),"transDate")  ;$p(^PAADM(episodeId),"^",6);d transRecord.SetAt($zd($p(transGlobal,"^",1),3),"transDate") ;
    d admRecord.SetAt(##class(websys.Conversions).TimeLogicalToHtml($p(transGlobal,"^",2)),"transTime")  ;$p(^PAADM(episodeId),"^",7)
    d admRecord.SetAt($p($g(^CTLOC(amdLocDR)),"^",2),"transTo")
    d admRecord.SetAt("","transFrom")
    d transRecords.Insert(admRecord)
    s firstToBed=0
    s sub="" 
    f  {
        s sub=$O(^Nur.Data.TransRecordI("TransDataState"," "_episodeId," N",sub)) 
        q:sub=""
        s transRecord=##class(%ArrayOfDataTypes).%New()
        d transRecord.SetAt(sub,"transID")
        d transRecord.SetAt($p($g(^SSU("SSUSR",+$LG(^Nur.Data.TransRecordD(sub),14))),"^",2),"transUser")
        d transRecord.SetAt(##class(websys.Conversions).DateLogicalToHtml($LG(^Nur.Data.TransRecordD(sub),12)),"transDate")  ;d transRecord.SetAt($zd($p(transGlobal,"^",1),3),"transDate") ;
        d transRecord.SetAt(##class(websys.Conversions).TimeLogicalToHtml($LG(^Nur.Data.TransRecordD(sub),13)),"transTime")
        s transType=$LG(^Nur.Data.TransRecordD(sub),3)
        i (transType="B")&&(firstToBed=0){
            d transRecord.SetAt("首次分床","transType")
            d transRecord.SetAt($LG(^Nur.Data.TransRecordD(sub),11),"transTo")
            d transRecord.SetAt("","transFrom")
            s firstToBed=1
        }
        elseif((transType="B")&&(firstToBed=1)) {
            d transRecord.SetAt("分床","transType")
            d transRecord.SetAt($LG(^Nur.Data.TransRecordD(sub),11),"transTo")
            s bedDesc=$LG(^Nur.Data.TransRecordD(sub),9)
            i bedDesc="" s bedDesc="等候区"
            d transRecord.SetAt(bedDesc,"transFrom")
        }
        elseif(transType="L"){
            d transRecord.SetAt("转科","transType")
            d transRecord.SetAt($p($g(^CTLOC($LG(^Nur.Data.TransRecordD(sub),5))),"^",2),"transTo")
            d transRecord.SetAt($p($g(^CTLOC($LG(^Nur.Data.TransRecordD(sub),4))),"^",2),"transFrom")
        }
        elseif(transType="W"){
            d transRecord.SetAt("转病区","transType")
            d transRecord.SetAt($p($g(^PAWARD($LG(^Nur.Data.TransRecordD(sub),7))),"^",2),"transTo")
            d transRecord.SetAt($p($g(^PAWARD($LG(^Nur.Data.TransRecordD(sub),6))),"^",2),"transFrom")
        }
        d transRecords.Insert(transRecord)
    }
    q transRecords
}

/// 获取患者的皮试医嘱
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Patient).GetSkinOrd("679"))
ClassMethod GetSkinOrd(episodeId As %String) As %ListOfDataTypes
{
    n (episodeId)
    s skinOrdList=##class(%ListOfDataTypes).%New()
    s oeordId=$o(^OEORD(0,"Adm",episodeId,""))
    q:oeordId="" skinOrdList
    k ordData
    s arcimDr=""
    f  s arcimDr=$O(^OEORDi(0,"ARCIM",oeordId,arcimDr)) q:arcimDr=""  d
    .q:(+arcimDr=0)
    .s itemCatDr=$P($G(^ARCIM(+arcimDr,$p(arcimDr,"||",2),1)),"^",10)
    .q:(itemCatDr="")
    .s ordTyp=$P($g(^ARC("IC",itemCatDr)),"^",7)
    .q:ordTyp'="R" //过滤掉非药品医嘱   
    .s date=""
    .f  s date=$O(^OEORDi(0,"ARCIM",oeordId,arcimDr,date)) q:date=""  d
    ..s oeoreId=0
    ..f  s oeoreId=$O(^OEORDi(0,"ARCIM",oeordId,arcimDr,date,oeoreId)) q:oeoreId=""  d
    ...s actId=$p($g(^OEORD(oeordId,"I",oeoreId,11)),"^",21)
    ...;q:actId=""  //无皮试备注
    ...s skinTest=$p($g(^OEORD(oeordId,"I",oeoreId,5)),"^",2)
    ...q:skinTest'="Y" //皮试勾
    ...s skinTestInfo=""
    ...s abnorm=$p($g(^OEORD(oeordId,"I",oeoreId,11)),"^",3)
    ...i abnorm="Y" s skinTestInfo="阳性"
    ...i abnorm="N"  s skinTestInfo="阴性"
    ...q:$G(skinTestInfo)=""
    ...s qfalg=0
    ...i $D(ordData(arcimDr)) d
    ....s skinTestInfo=$P(ordData(arcimDr),"^",1)
    ....i skinTestInfo="阳性" s qfalg=1
    ...q:qfalg=1
    ...s ordData(arcimDr)=skinTestInfo_"^"_date
    
    s carcimDr=""
    f  s carcimDr=$O(ordData(carcimDr)) q:carcimDr=""  d
    .s stDate=$ZD($P(ordData(carcimDr),"^",2),3)
    .s skinTestInfo=$P(ordData(carcimDr),"^",1)
    .s arcimName=##class(web.UDHCJFCOMMON).GetDrugCommonNameByArcimId(carcimDr)
    .s ordInfo=##class(%ArrayOfDataTypes).%New()
    .d ordInfo.SetAt(stDate,"stDate")
    .d ordInfo.SetAt(skinTestInfo,"skinTestInfo")
    .d ordInfo.SetAt(arcimName,"arcimName")
    .d skinOrdList.Insert(ordInfo)
    q skinOrdList
}

}
